package 临时工具.shangyu2;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.digitenjoy.common.core.utils.GPSDataConvertUtil;
import lombok.Data;
import 临时工具.ExportExcel;

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

@Data
public class yhd implements Serializable {
    //读取文件
    static String filePath = "C:\\Users\\瑾瑜\\Desktop\\sql\\front\\source\\yh\\PSGWYHSJ.geojson";
    static String outPath = "C:\\Users\\瑾瑜\\Desktop\\sql\\front\\source\\yh\\PSGWYHSJ_out.xlsx";
    public static void main(String[] args) throws IOException {
        String content = FileUtil.readUtf8String(filePath);
        JSONObject jsonObject = JSONObject.parseObject(content);
        JSONArray features = jsonObject.getJSONArray("features");
        Set<String> set = new HashSet<>();
        JSONArray list = new JSONArray();
        for (int i = 0; i < features.size(); i++) {
            JSONObject feature = features.getJSONObject(i);
            JSONObject properties = feature.getJSONObject("properties");
            JSONArray coordinates = feature.getJSONObject("geometry").getJSONArray("coordinates").getJSONArray(0);

            JSONObject object = new JSONObject();
            for (String key:properties.keySet()) {
                if ("objectid".equals(key.toLowerCase())){
                    continue;
                }
                object.put(key.toLowerCase(), properties.getString(key));
            }



            JSONArray start = coordinates.getJSONArray(0);
            JSONArray end = coordinates.getJSONArray(1);
            object.put("yhkszbjd84", start.getString(0));
            object.put("yhkszbwd84", start.getString(1));
            object.put("yhjszbjd84", end.getString(0));
            object.put("yhjszbwd84", end.getString(1));
            double[] doubles1 = GPSDataConvertUtil.transformWGS84ToGCJ02(start.getDouble(0), start.getDouble(1));
            double[] doubles2 = GPSDataConvertUtil.transformWGS84ToGCJ02(end.getDouble(0), end.getDouble(1));
            object.put("yhkszbjd", String.valueOf(doubles1[0]));
            object.put("yhkszbwd", String.valueOf(doubles1[1]));
            object.put("yhjszbjd", String.valueOf(doubles2[0]));
            object.put("yhjszbwd", String.valueOf(doubles2[1]));

            object.put("yhsp", clearUrl( object.getString("yhsp")));
            object.put("yhzp", clearUrl( object.getString("yhzp")));
            object.put("jcrq", object.getString("jcrq").replaceAll("T", " "));

            list.add(object);
            set.addAll(object.keySet());
        }

        ex(set, list);

    }
    private static void ex(Set<String> set, JSONArray list ) throws IOException {
        ExportExcel exportExcel = new ExportExcel();

        ArrayList<String> arr = new ArrayList<>(set);

        for(int j = 0; j < arr.size(); j++){
            exportExcel.setValue(j, arr.get(j));
        }
        exportExcel.addRowIndex();
        for(int i = 0; i < list.size(); i++){
            JSONObject obj = list.getJSONObject(i);
            for(int j = 0; j < arr.size(); j++){
                exportExcel.setValue(j, obj.getString(arr.get(j)));
            }
            exportExcel.addRowIndex();
        }
        exportExcel.write(Files.newOutputStream(new File(outPath).toPath()));
    }

    private static String clearUrl(String url) {
        return "http://39.184.194.199:9200/file/gwjc/"+url.replaceAll("\\\\", "/");
    }
}
